﻿// JScript File
_webfolder='';
_path='/';
_parent='/';
_rootUrl='/swa/i';
_fileUploadState='waiting';
var _serviceUrl='/manager/dialogs/ImageServices.aspx';
var _selectedImage='';

var ctrlFileUpload=null;
var ctrlUploadFolder=null;

function onPageInit() {
}

Event.observe(window, 'load', function() {
    if ($('currentfolder') != null)
        $('currentfolder').textContent = _path;
    ctrlUploadFolder.value = _path;
    if ($('parent') != null)
        $('parent').style.display='none';
    refresh();
});

function fileChange()
{
    if ($('fileUpload').value == '' || _fileUploadState == 'uploading')
        return;
        
    $('state').innerHTML = '<i>Uploading...</i>';
    _fileUploadState = 'uploading';
    $('fileUpload').disabled=true;
}

function moveTempFileToActiveFolder(filename)
{
    TanashCMS.Services.SelectImage.moveTempFileToFolder(filename, _rootUrl+_path, onMoveFileSuccess, onMoveFileFailed);
}
function onMoveFileSuccess()
{
}
function onMoveFileFailed()
{
}

function refresh()
{
    $('imagesLoading').style.display='block';
    
    //TanashCMS.Services.SelectImage.GetFolders(_rootUrl+_path, onGetFolders);
    
    new Ajax.Request(_serviceUrl + '?method=GetImages&path='+_path+'&webfolder='+_webfolder,
    {
        method:'get',
        onSuccess: onGetFilesSuccess
    });
    //TanashCMS.Services.SelectImage.getFileListHtml(_webfolder, _path, onGetFilesHtml);
    
}

function onGetFilesSuccess(transport)
{
$('images').innerHTML = transport.responseText;
$('imagesLoading').hide();
}

function onGetFolders(transport)
{
    var ef = $('folders');
    while(ef.childNodes.length > 0)
        ef.removeChild(ef.childNodes[0]);
    for(var i=0; i<directories.length; i++)
    {
        var fl = document.createElement('a');
        fl.setAttribute('class', 'folder');
        fl.textContent = directories[i].Name;
        fl.setAttribute('href', 'javascript:void(0);');
        fl.setAttribute('onclick', 'onChangeFolder("'+directories[i].Name+'");');
        ef.appendChild(fl);
    }
    
}

function onGetFiles(files)
{
    TanashCMS.Services.SelectImage.getFileListHtml(_webfolder, _path, onGetFilesHtml, onGetFilesHtmlFail);
}

function onGetFilesHtmlFail(err)
{
    alert(err._description);
}

function onGetFilesHtml(html)
{
    var ei = $('images');
    $('imagesLoading').hide();
    ei.innerHTML = html;

}

function folderUp()
{
    $('imagesLoading').show();

    $('parent').style.display='block';
    var _parentPath = "";
    var _pathArray = _path.split('/');
    for (var i=0; i<_pathArray.length-2; i++)
    {
        _parentPath = '/' + _pathArray[i];
    }
    _path = _parent;
    _parent = _parentPath;
    
    $('currentfolder').textContent = unescape(_path);
    ctrlUploadFolder.value = unescape(_path);
    //$('parent').setAttribute('onclick', 'onChangeFolder('+_parent+');');

    refresh();
}

function onChangeFolder(newPath)
{
    $('imagesLoading').show();

    $('parent').style.display='block';
    _parent = _path;
    _path = newPath;
    
    $('currentfolder').textContent = unescape(_path);
    ctrlUploadFolder.value = unescape(_path);
    //$('parent').setAttribute('onclick', 'onChangeFolder('+_parent+');');

    refresh();
}

function select(relativeImageUrl, width, height)
{
    var preview = $('previewImage');
    while (preview.childNodes.length > 0)
        preview.removeChild(preview.childNodes[0]);
    
    var image = document.createElement('img');
    image.setAttribute('src', '/services/thumbnail.aspx?sx=300&method=restricttosize&i='+_rootUrl+relativeImageUrl);
    preview.appendChild(image);
    
    var info = $('imageInfo');
    info.innerHTML = '<b>'+unescape(relativeImageUrl)+'</b>';
    if (!isNaN(width) && !isNaN(height))
    {
        info.innerHTML += '<br/>Dimensions: ' + width + ' x ' + height;
    }
    
    var btnConfirm = $('btnUseImage');
    btnConfirm.textContent = 'Use This Image';
    btnConfirm.setAttribute('onclick', 'onUseThisImage("'+relativeImageUrl+'"); return false;');
    
    $('preview').style.display='block';
    
}

function onUseThisImage(relativeImageUrl)
{
    window.top.selectedImage = _rootUrl+relativeImageUrl;
    window.top.hidePopWin(true);
    
}


function startCallback() 
{
    $("processDiv").style.display = "block";// the progress div
    $("fileName").innerText = ctrlFileUpload.value;// set file name in the progress div
    return true;
}

function completeCallback(response) 
{
   //moveTempFileToActiveFolder(response);
   $("processDiv").style.display = "none";// hide progresss div
   refresh();
}